var express = require('express');
var mongoose = require('mongoose');
var url = require('url');

var Setting = require('../models/setting.js');

require('../models/user.js');
var User = mongoose.model('User');

var router = express.Router();
router.get('/start', function (req, res) {
	res.render('start');
});

router.get('/', function (req, res) {
    var User = mongoose.model('User');
	var query = url.parse(req.url, true).query;
    var sid = query.sid; //用户ID
    var uname = query.unm || ''; //用户姓名
    var utt = query.utt || ''; //用户所在部门

    if (req.session && req.session.user && (sid == req.session.user.sid)) {
        var u = req.session.user;
        if(u.name !== uname || u.profession !== utt){ //当用户的名字和岗位发生改变后
            User.findByIdAndUpdate(u._id, {
                name : uname,
                profession: utt
            }, {}, function(err, newUser){
                if(err) throw err;
                req.flash('success', '登陆成功');
                console.log('【' + sid + '】登陆成功');
                req.session.user = newUser;
                console.log(Setting.getData());
                res.render('index', {
                    user: req.session.user,
                    settings : Setting.getData()//系统设置
                });
            });
        } else {
            res.render('index', {
                user: req.session.user,
                //系统设置
                settings : Setting.getData()});
        }
    } else {
        if (sid == undefined) {
            req.flash('success', '错误的sid');
            res.render('index');
        } else {
            User.findOne({
                sid: sid
            }, function (err, user) {
                console.log("当前登陆信息:" + JSON.stringify(user));
                console.log(err);
                if (err || user == null) {
                    var user = new User();
                    user.sid = sid;
                    user.name = uname;
                    user.profession = utt;
                    user.score = 0;
                    user.battles = 0;
                    user.success = 0;
                    user.failure = 0;
                    user.flee = 0;
                    user.save(function (err, newUser) {
                        if(err) throw err;
                        req.flash('success', '登陆成功');
                        console.log('【' + sid + '】登陆成功');
                        req.session.user = newUser;
                        console.log(Setting.getData());
                        res.render('index', {
                            user: req.session.user,
                            settings : Setting.getData()//系统设置
                        });
                    });
                } else {
                    if(user.name !== uname || user.profession !== utt){ //当用户的名字和岗位发生改变后
                        User.findByIdAndUpdate(user._id, {
                            name : uname,
                            profession: utt
                        }, {}, function(err, newUser){
                            if(err) throw err;
                            req.flash('success', '登陆成功');
                            console.log('【' + sid + '】登陆成功');
                            req.session.user = newUser;
                            console.log(Setting.getData());
                            res.render('index', {
                                user: req.session.user,
                                settings : Setting.getData()//系统设置
                            });
                        });
                    } else {
                        req.flash('success', '登陆成功');
                        console.log('【' + sid + '】登陆成功');
                        req.session.user = user;
                        console.log(Setting.getData());
                        res.render('index', {
                            user: req.session.user,
                            //系统设置
                            settings : Setting.getData()
                        });
                    }
                }
            });
        }
    }
});

//荣誉榜
router.post('/honor', function (req, res) {
    var query = req.query;
    var skip = query.skip;
    var limit = query.limit;
    User.find({
        score: {
            $gt: 0
        }
    }).sort({
        score: 'desc'
    }).skip(skip).limit(limit).exec(function (err, users) {
        res.send(users);
    })
});

//获取用户信息
router.post('/user/:userId', function(req, res){
    var userId = req.params.userId;
    User.findById(userId).exec(function(err, data){
        if(err) throw err;
        res.send(data);
    });
});

//设置头像
router.post('/userSetPhoto', function(req, res){
    var query = req.query;
    console.log(req.query);
    var userId = query.userId;
    var photo = query.photo;
    User.findByIdAndUpdate(userId, {
        photo : photo
    }, {}, function(err, data){
        if(err) throw err;
        console.log(data);
        res.send(data);
    });
});

router.get('/templates/tabs', function (req, res) {
    res.render('templates-tabs');
});

router.get('/templates/tab/home', function (req, res) {
    var user = req.session && req.session.user ? req.session.user : null;
    res.render('templates-tab-home', {user: user});
});

router.get('/templates/tab/honor', function (req, res) {
    res.render('templates-tab-honor');
});

router.get('/templates/tab/mybattles', function (req, res) {
    res.render('templates-tab-mybattles');
});

router.get('/templates/tab/mybattles/newletter', function (req, res) {
    res.render('templates-tab-mybattles-newletter');
});

router.get('/templates/tab/mybattles/inbox', function (req, res) {
    res.render('templates-tab-mybattles-inbox');
});

router.get('/templates/tab/mybattles/outbox', function (req, res) {
    res.render('templates-tab-mybattles-outbox');
});

router.get('/templates/tab/other', function (req, res) {
    var user = req.session && req.session.user ? req.session.user : null;
    res.render('templates-tab-other', {user: user});
});

router.get('/templates/tab/other/manual', function (req, res) {
    res.render('templates-tab-other-manual');
});

router.get('/templates/tab/store/detail', function (req, res) {
    res.render('templates-tab-store-detail');
})

router.get('/templates/tab/battle/detail', function (req, res) {
    res.render('templates-tab-battle-detail');
})

router.get('/templates/tab/practice/detail', function (req, res) {
    res.render('templates-tab-practice-detail');
})

module.exports = router;